package com.hmkx.app.util.comon;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.*;
import java.util.Properties;


public class ConfigManger
{
	private static Log log = LogFactory.getLog(ConfigManger.class);

	private static ConfigManger configManager = null;
	private static Properties property = null;
	private static Properties pathProperty = null;

	private ConfigManger()
	{
		
	}

	/**
	 * @Title: init
	 * 
	 * @Description: 初始化此类
	 * @return void 返回类型
	 * @date 2011-11-23 下午04:08:32
	 * 
	 * @author kyj
	 */
	public static void init()
	{
		configManager = null;
		property = null;
		pathProperty = null;
	}

	/**
	 * @Title: getProperty
	 * 
	 * @Description: 初始化property
	 * @return Properties 返回类型
	 * @date 2011-10-17 下午04:46:27
	 * 
	 * @author kyj
	 */
	public Properties getProperty()
	{
		if (property == null || property.isEmpty())
		{
			property = new Properties();
			
			String filePath = this.getClass().getResource("/").getPath()
								+ "memcacheClient.properties";
			
			File file = new File(filePath);
			if (!file.exists())
			{
			   filePath ="" + File.separator
					+ "memcacheClient.properties";
				 
				file = new File(filePath);
			}
			
			log.debug("filePath===" + filePath);

			try
			{
				property.load(new FileInputStream(filePath));
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		
		return property;
	}
	/**
	 * @Title: getProperty
	 * 
	 * @Description: 初始化property
	 * @return Properties 返回类型
	 * @date 2011-10-17 下午04:46:27
	 * 
	 * @author kyj
	 */
	public Properties getPathProperty()
	{
		if (pathProperty == null || pathProperty.isEmpty())
		{
			pathProperty = new Properties();
			
			String filePath = this.getClass().getResource("/").getPath()
			+ "config.properties";
			
			File file = new File(filePath);
			if (!file.exists())
			{
				filePath ="" + File.separator
				+ "config.properties";
				
				file = new File(filePath);
			}
			
			log.debug("filePath===" + filePath);
			
			try
			{
				pathProperty.load(new FileInputStream(filePath));
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		
		return pathProperty;
	}

	/**
	 * @Title: getInstance
	 * 
	 * @Description: 实例化一个configManager的对象
	 * @return ConfigManager 返回类型
	 * @date 2011-10-17 下午04:31:59
	 * 
	 * @author kyj
	 */
	public static ConfigManger getInstance()
	{
		if (configManager == null)
		{
			configManager = new ConfigManger();
		}
		return configManager;
	}

	/**
	 * @Title: modifyProperties
	 * 
	 * @Description: 修改properties中的键值
	 * @param filePath
	 * @param key
	 * @param value
	 * @return void 返回类型
	 * @date 2011-11-23 下午01:03:55
	 * 
	 * @author kyj
	 */
	public void modifyProperties(String key, String value)
	{
		Properties prop = new Properties();
		try
		{
			String filePath ="" + File.separator
					+ "hwei_config.properties";
			File file = new File(filePath);
			if (!file.exists())
			{
				filePath = this.getClass().getResource("/").getPath()
						+ "hwei_config.properties";
				file = new File(filePath);
			}
			InputStream fis = new FileInputStream(file);
			prop.load(fis);
			// 一定要在修改值之前关闭fis
			fis.close();
			OutputStream fos = new FileOutputStream(filePath);
			prop.setProperty(key, value);
			// 保存，并加入注释
			prop.store(fos, "Update '" + key + "' value");
			fos.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * @Title: getString
	 * 
	 * @Description: 获得key的string值
	 * @return String 返回类型
	 * @date 2011-12-28 下午03:11:02
	 * 
	 * @author kyj
	 */
	public String getString(String key)
	{
		String value = null;
		
		if(key != null && !key.trim().equals(""))
		{
			Properties propertyes = this.getProperty();
			value = propertyes.getProperty(key);
		}
		
		return value;
	}
	
	/**
	 * @Title: getString
	 * 
	 * @Description: 获得key的string值
	 * @return String 返回类型
	 * @date 2011-12-28 下午03:11:02
	 * 
	 * @author kyj
	 */
	public String getString(String key, String defalutString)
	{
		String value = defalutString;
		
		if(key != null && !key.trim().equals(""))
		{
			Properties propertyes = this.getProperty();
			value = propertyes.getProperty(key,defalutString);
		}
		
		return value;
	}

	/**
	 * @Title: getInteger
	 * 
	 * @Description: 获得key的string值
	 * @return String 返回类型
	 * @date 2011-12-28 下午03:11:02
	 * 
	 * @author kyj
	 */
	public Integer getInteger(String key)
	{
		Integer value = null;
		
		if(key != null && !key.trim().equals(""))
		{
			String str = this.getString(key);
			
			if(str != null && str.matches("^[-|0-9][0-9]*$"))
			{
				value = Integer.parseInt(str);
			}
		}
		
		return value;
	}
	/**
	 * @Title: getInteger
	 * 
	 * @Description: 获得key的string值
	 * @return String 返回类型
	 * @date 2011-12-28 下午03:11:02
	 * 
	 * @author kyj
	 */
	public Integer getInteger(String key, Integer defalutInt)
	{
		Integer value = defalutInt;
		
		if(key != null && !key.trim().equals(""))
		{
			String str = this.getString(key);
			
			if(str != null && str.matches("^[-|0-9][0-9]+$"))
			{
				value = Integer.parseInt(str);
			}
		}
		
		return value;
	}
	
	/**
	 * @Title:getPath 获得文件路径
	 * @Description:TODO
	 * @param pathKey
	 * @return
	 * @return String
	 * @author kyj
	 * @date Jul 11, 2012
	 */
	public String getPath(String pathKey)
	{
		String value = "";
		
		if(pathKey != null && !pathKey.trim().equals(""))
		{
			Properties propertyes = this.getPathProperty();
			value = propertyes.getProperty(pathKey);
		}
		
		return value;
	}
}
